package com.hexseek.libretvcloud.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class HttpRequestUtils {

    @Autowired
    private RestTemplate restTemplate;

    public String get(String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAll(RandomHttpHeadersUtil.generateRandomHeaders());
        HttpEntity<?> entity = new HttpEntity<>(headers);

        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                entity,
                String.class
        );

        return response.getBody();
    }

    public String post(String url, Object body) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAll(RandomHttpHeadersUtil.generateRandomHeaders());
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<?> entity = new HttpEntity<>(body, headers);

        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                entity,
                String.class
        );

        return response.getBody();
    }
}
